package com.sgai.zero_robot.ui.alarm;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.charset.Charset;

/**
 * Describe :
 * Created by zzw on 2024/8/28
 */
public class FileUtils {
    /**
     * 创建日志文件
     * @param file 日志文件
     * @return  true 创建成功   false 创建失败
     */
    public static boolean createFile(File file){
        if (file == null){
            return false;
        }
        File file1 = file.getParentFile();
        if (!file1.exists()) {
            file1.mkdirs();
        }
        if (!file.exists()) {
            try {
                file.createNewFile();
                return true;
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
        return false;
    }
    public static void saveStringToFile(File file, String builder) {
        if (file.exists()) {
            FileOutputStream fos = null;
            try {
                fos = new FileOutputStream(file, true);
                fos.write(builder.getBytes(Charset.defaultCharset()));
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } finally {
                try {
                    if (fos != null) {
                        fos.flush();
                        fos.close();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
